<?php
class st_appperm_model extends stmodel_2_1{

    public function __construct($config) {
        parent::__construct ( $config );
        $this->setTableName($this->prefix.'st_app_perm');
        $this->setPrimaryKey('id');
    }    
        
    public function getRecords($args = '') {
        $default_args = array(
            'fields' => '*',
            'and_where' => '',
            'or_where' => '',
            'where' => '',
            'order' => '',
            'limit' => '',
            'group_by' => '',
            'custom_query' => '',
        );
        if ($args == '')
            $args = $default_args;

        $fields = $args['fields'] ? $args['fields'] : '*';
        $and_where = $args['and_where'] ? $args['and_where'] : '';
        $or_where = $args['or_where'] ? $args['or_where'] : '';
        $custom_where = $args['where'] ? $args['where'] : '';
        $order = $args['order'] ? $args['order'] : '';
        $limit = $args['limit'] ? $args['limit'] : '';
        $group_by = $args['group_by'] ? $args['group_by'] : '';
        $custom_query = $args['custom_query'] ? $args['custom_query'] : '';
        if (!empty($custom_query)) {
            $query = $custom_query;
        } else {
            if ($fields != '*')
                $fields = implode(', ', $fields);
            //
            $where = ' WHERE ';

            if ($and_where != '')
                $where .= implode('', $and_where);
            if ($or_where != '')
                $where .= implode('', $or_where);
            if ($custom_where != '')
                $where .= $custom_where;
            if ($where == ' WHERE ')
                $where = '';
            //
            if ($order != '')
                $order = ' ORDER BY ' . implode(' ', $order);
            //
            if ($limit != '')
                $limit = ' LIMIT ' . implode(', ', $limit);
            if ($group_by != '')
                $group_by = ' GROUP BY ' . $group_by;
            //

            $table_user = $this->prefix.'st_social_user';
            $table_wp_user = $this->prefix.'users';
            $query = "SELECT $fields
            FROM $this->_tableName app
            RIGHT JOIN $table_user user on user.user_id = app.user_id
            JOIN $table_wp_user wp_user on wp_user.ID = user.user_id       
            $where
            $group_by
            $order
            $limit
            ";
        }
//        echo $query;
        return $this->get_results($query);
    }
    
    public function enableApplication($data){
        $data['id'] = $this->getID($data);
        if($this->saveRecords($data)){
            $arg = array(
                'status'    => 'success',
                'message'   => __('Add permission successful', $config['plugin_name'])
            );
        }else{
            $arg = array(
                'status'    => 'error',
                'message'   => __('Add permission unsuccessful', $config['plugin_name'])
            );
        }
        return $arg;
    }

    public function disableApplication($data){
        $id = $this->getID($data);
        if($this->deleteRecords($id)){
            $arg = array(
                'status'    => 'success',
                'message'   => __('Remove permission successful', $config['plugin_name'])
            );
        }else{
            $arg = array(
                'status'    => 'error',
                'message'   => __('Remove permission unsuccessful', $config['plugin_name'])
            );
        }
        return $arg;
    }

    public function getID($data){
        $query = "SELECT id FROM `{$this->_tableName}`
        WHERE `user_id` = {$data['user_id']} AND `app_name` like('{$data['app_name']}')
        ";
        return $this->get_var($query);
    }

    public function countRecords($args = ''){

        $result = $this->getRecords($args);
        return count($result);

    }
}